/**
 * 
 */
package com.ebrg.secure9.mail.security;

import java.util.ArrayList;

import com.ebrg.secure9.R;

import android.content.Context;

/*
 * String usage in this class is safe, as we're not actually reading key
 * data, but rather string filenames and checking for existance of said keys.
 */
public class Keys{

	// List to hold recipient addresses for which keys exist. Instantiated at 2 
	public static ArrayList<String> keyList = new ArrayList<String>(2);
	public static String[] keyArray;
	public static ArrayList<String> initialKeys = new ArrayList<String>(2);	
	
	
	//Re-input all key files to keyList 
	//keyExists() automatically calls this.  The only time it should need to be called
	//Manually is for displaying the "Manage Keys" window showing all account keys.
	public static void refreshKeys(Context context)
	{
		keyList.clear();
		//Load files into key array (all files)
		keyArray = context.fileList();
		
		//Search array for .key files, adding them to the keyList.
		for(int i = 0; i < keyArray.length; i++)
			if(keyArray[i].endsWith(context.getString(R.string.complete_key_file_type)))
				keyList.add(keyArray[i]);		
	}
	
	//Only called by MessageView.java - only used to retrieve inital key
	//info and finish setup based on returned values from remote user.
	public static void refreshInitialKeys(Context context)
	{
		initialKeys.clear();
		//Load files into key array (all files)
		keyArray = context.fileList();
		
		//Search array for .key files, adding them to the keyList.
		for(int i = 0; i < keyArray.length; i++)
			if(keyArray[i].endsWith(context.getString(R.string.initial_key_file_type)))
				initialKeys.add(keyArray[i]);		
	}
	
	//Find key for any given remote/local email account combo 
	public static boolean keyExists(Context context, String localEmail, String remoteEmail)
	{
		refreshKeys(context);		
		if(Keys.keyList.contains(localEmail + remoteEmail + context.getString(R.string.complete_key_file_type)))
			return true;
		else
			return false;
	}
	
	//Called by Accounts.java - when user removes an account we delete all their 
	//related keys.
	public static void deleteAccountKeys(Context context)
	{
		// To do		
	}
	
	
	
	
	
	
}
